<?php

/**
 * home actions.
 *
 * @package    SXShopCartPlugin
 * @subpackage home
 * @author     SoftXpert
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class shopCartAdminSettingActions extends SXActions
{

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function preExecute()
    {
        Settings::encrypt('123456'); 
    }

    /**
     * Login action
     * @param sfWebRequest $request 
     * @author Mina Gerges
     */
    public function executeLogin(sfWebRequest $request)
    {
        $redirectUrl = 'shopCartAdminProducts/index';
        if ($this->getUser()->isAuthenticated())
        {
            $this->redirect($redirectUrl);
        }
        $this->loginForm = new LoginForm();
        if ($request->isMethod(sfWebRequest::POST))
        {
            $this->loginForm->bind($request->getParameter($this->loginForm->getName()));
            if ($this->loginForm->isValid())
            {
                // fro rember me
                if ($request->getParameter('remember'))
                {
                    $random   = md5(time() * rand(2, 2000000000));
                    $settings = $this->loginForm->getSettingsObject();
                    $settings->setRememberMeCode($random);
                    $settings->save();
                    // add cookie named in the browser
                    sfContext::getInstance()->getResponse()->setCookie(sfConfig::get('app_cookie_name'), $random, time() + 60 * 60 * 24 * 365, '/');
                }
                $this->getUser()->setAuthenticated(true);
                if ($request->getPostParameter('url') && $request->getPostParameter('url') != $this->generateUrl('Login', array(), true))
                {
                    $redirectUrl = $request->getPostParameter('url');
                }
                $this->redirect($redirectUrl);
            }
        }
    }

    /**
     * logout action 
     * @author Mina Gerges
     */
    public function executeLogout()
    {
        // clear all session opened for this user
        $this->getUser()->getAttributeHolder()->clear();
        sfContext::getInstance()->getUser()->setAuthenticated(false);

        // remove cookie
        sfContext::getInstance()->getResponse()->setCookie(sfConfig::get('app_cookie_name'), '', time() - 60, '/');

        // redirect to login page
        $this->redirect('shopCartAdminProducts/index');
    }

    /**
     * edit user settings (username , email , password)
     * @param sfWebRequest $request 
     * @author Mina Gerges
     */
    public function executeEditSettings(sfWebRequest $request)
    {
        $settings   = SettingsTable::getInstance()->findAll()->getFirst();
        $this->form = new UserSettingsForm($settings);
        if ($request->isMethod(sfWebRequest::POST))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                $this->form->save();
                $this->getUser()->setFlash('message_success', 'update settings data succssed');
            }
        }
    }
}